﻿using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Read.API.Command.UserCommand;
using B.S.BaseData.Read.API.DTO;
using MediatR;

namespace B.S.BaseData.Read.API.CommandHandler.UserHandler
{
    public class GetUserHandler : IRequestHandler<GetUserCommand, APIPaging<UserDTO>>
    {
        private readonly IBaseRepository<User> userRep;
        private readonly IBaseRepository<UserRole> urRep;
        private readonly IBaseRepository<Role> roleRep;

        public GetUserHandler(IBaseRepository<User> userRep,
            IBaseRepository<UserRole> urRep,
            IBaseRepository<Role> roleRep)

        {
            this.userRep = userRep;
            this.urRep = urRep;
            this.roleRep = roleRep;
        }
        public Task<APIPaging<UserDTO>> Handle(GetUserCommand request, CancellationToken cancellationToken)
        {
            APIPaging<UserDTO> result = new APIPaging<UserDTO>();
            var linq = userRep.GetAll().Where(x => !x.IsDeleted);
            var list = from a in linq
                       join b in urRep.GetAll() on a.UserId equals b.UserId
                       join c in roleRep.GetAll() on b.RoleId equals c.RoleId
                       select new UserDTO
                       {
                           UserId = a.UserId,
                           Name = a.Name,
                           Sex = a.Sex,
                           Province = a.Province,
                           City = a.City,
                           County = a.County,
                           Address = a.Address,
                           UpdateTime = a.UpdateTime,
                           UpdateUser = a.UpdateUser,
                           CreateTime = a.CreateTime,
                           CreateUser = a.CreateUser,
                           Importance=a.Importance,
                           RoleName = c.RoleName,
                           RoleId = c.RoleId,
                       };

            result.Code = APIEnum.Success;
            result.Msg = "显示成功";
            result.TotalCount = linq.Count();
            result.PageCount = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            var lista = list.ToList();
            var s = lista.GroupBy(t => t.UserId);
            var t = new List<UserDTO>();
            foreach (var item in s)
            {
                string s1 = "";
                foreach (var items in item)
                {
                    if (s1 == "")
                    {
                        s1 += items.RoleName;
                    }
                    else
                    {
                        s1 += "," + items.RoleName;
                    }
                }
                item.FirstOrDefault().RoleNames = s1;
            }

            foreach (var item in s)
            {
                t.Add(item.FirstOrDefault());
            }
            result.Items = t.OrderByDescending(p=>p.UserId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return Task.FromResult(result);
        }
}

}
